package edu.gyc.hicloud.payment1.controller;





import edu.gyc.hicloud.payment1.model.MyResult;
import edu.gyc.hicloud.payment1.model.Payment;
import edu.gyc.hicloud.payment1.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-03-14
 */
@RestController
@Slf4j
@RequestMapping("/payment")

public class PaymentController {
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery")
    public Object discovery() {
        List<String > services=discoveryClient.getServices();
        for (String s : services) {
            log.info("****service:"+s);
        }
      List<ServiceInstance> instances=  discoveryClient.getInstances("cloud-payment");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+
                    instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
    @Autowired
    private PaymentService paymentService;


    @Value("${server.port}")
    private String serverPort;

    @GetMapping("get/{id}")
    public Object get(@PathVariable(name = "id") Long id){
        log.info("*******查询id："+id);
       Payment payment=paymentService.getById(id);
        return new MyResult<Payment>(200,"查询成功，serverPort："+serverPort,payment);

    }
    @PostMapping("/create")
    public MyResult create(@RequestBody Payment payment) {
        log.info("******input payment:"+payment);
        boolean r = paymentService.save(payment);
        log.info("******插入结果："+r);
        if(r){
            return new MyResult(200,"插入数据成功,serverPort:"+serverPort,r);

        }else {
            return new MyResult(444,"插入数据失败");
        }
    }
}

